/*
 * Copyright (C) 2011 Emmanuel Tourdot
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 * $Id$
 */
package org.etourdot.xinclude;

import net.sf.saxon.s9api.Processor;
import org.etourdot.xinclude.stax.XIncProcStaxFactory;
import org.etourdot.xinclude.xpointer.XPointerEngine;

/**
 * @author Emmanuel Tourdot
 */
public class XIncProcConfiguration
{
    public static final String XINCLUDE_INCLUDE = "include";
    public static final String XINCLUDE_FALLBACK = "fallback";

    public static final String XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID
            = "http://etourdot.org/xml/features/xinclude/fixup-base-uris";
    public static final String XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID
            = "http://etourdot.org/xml/features/xinclude/fixup-language";
    private boolean baseUrisFixup = false;
    private boolean languageFixup = false;
    private String xincfactory;
    private XIncProcAbstractFactory factory;
    private XIncProcEngineHandler engine;
    private Processor processor;
    private XPointerEngine xPointerEngine;

    public XIncProcConfiguration(final Processor processor)
    {
        this.processor = processor;
    }

    public XIncProcConfiguration()
    {
        this.processor = new Processor(false);
    }

    public XIncProcEngineHandler getEngine()
    {
        if (engine == null)
        {
            engine = getFactory().newEngine();
        }
        return engine;
    }

    public XIncProcAbstractFactory getFactory()
    {
        if (factory == null)
        {
            factory = new XIncProcStaxFactory();
            if (xincfactory != null)
            {
                try {
                    factory = (XIncProcAbstractFactory) getClass().getClassLoader().loadClass(xincfactory).newInstance();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            factory.setConfiguration(this);
        }
        return factory;
    }

    public XPointerEngine getXPointerEngine()
    {
        if (xPointerEngine == null)
        {
            xPointerEngine = new XPointerEngine(this);
        }
        return xPointerEngine;
    }

    public void setConfigurationProperty(String name, Object value)
    {
        if (XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID.equals(name))
        {
            if (value instanceof Boolean)
            {
                baseUrisFixup = (Boolean) value;
            }
            else if (value instanceof String)
            {
                baseUrisFixup = Boolean.getBoolean((String) value);
            }
        }
        if (XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID.equals(name))
        {
            if (value instanceof Boolean)
            {
                languageFixup = (Boolean) value;
            }
            else if (value instanceof String)
            {
                languageFixup = Boolean.getBoolean((String) value);
            }
        }
    }

    public Object getConfigurationProperty(String name)
    {
        if (XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID.equals(name))
        {
            return baseUrisFixup;
        }
        if (XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID.equals(name))
        {
            return languageFixup;
        }
        return null;
    }

    public boolean isBaseUrisFixup() {
        return this.baseUrisFixup;
    }

    public void setBaseUrisFixup(final boolean baseUrisFixup)
    {
        this.baseUrisFixup = baseUrisFixup;
    }

    public boolean isLanguageFixup() {
        return this.languageFixup;
    }

    public void setLanguageFixup(final boolean languageFixup)
    {
        this.languageFixup = languageFixup;
    }

    public void setXIncProcFactory(final String className)
    {
        this.xincfactory = className;
    }

    public String getXIncProcFactory()
    {
        return this.xincfactory;
    }

    public Processor getProcessor()
    {
        return processor;
    }

    public void setProcessor(final Processor processor)
    {
        this.processor = processor;
    }
}
